﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BathhouseService.Areas.CURD.ViewModels.ReportFormVMs;
using BathhouseService.Areas.WX.Models.HomeVMs;
using BathhouseService.Models.Commonality;
using BathhouseService.Models.SqlModels;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using WalkingTec.Mvvm.Mvc;

namespace BathhouseService.Areas.CURD.Controllers
{
    [Area("CURD")]
    [ActionDescription("报表服务")]
    public class ReportFormController : BaseController
    {
        /// <summary>
        /// 服务月度报表
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            var vm = CreateVM<ServiceReportFormListVM>();
            return PartialView(vm);
        }

        [ActionDescription("搜索")]
        [HttpPost]
        public string Search(ServiceReportFormListVM vm)
        {
            return vm.GetJson(false);
        }
        [ActionDescription("服务详情")]
        public async Task<IActionResult> Details(Guid ID,string date)
        {
            var datetime = Convert.ToDateTime(date + "-05");
            var startMonth = DateCalculation.StartMonth(datetime);
            var endMonth = DateCalculation.EndMonth(datetime);
            var list = await DC.Set<ServiceRecord>()
                .Include(x => x.ValueAddedServices)
                .Where(x => x.ServicePersonId == ID && x.PaymentTime > startMonth && x.PaymentTime < endMonth)
                .GroupBy(x => x.ValueAddedServices.Names)
                  .Select(x => new PerformanceAppraisalDetailedVMs
                  {
                      Name = x.Key,
                      Count = x.Count()
                  })
                  .ToListAsync();
            ViewBag.username = DC.Set<FrameworkUserBase>().SingleOrDefault(x => x.ID == ID).Name;
            return PartialView(list);
        }



    }
}